<?php
class Add extends Controller {
	public function json() {
		$app = 'productImage';
		$config = include SERVER_PATH.'/config/upload.php';
		
		
		$id = $this->post('id', 'int');
		if(!$id) $this->displayJSON(array('status' => -1, 'message' => '未知景点'));
		
		$images = $this->post('images', 'arr');
		if(!$images) $this->displayJSON(array('status' => -2, 'message' => '请选择图片'));
		$cols = array();
		$path = 'images/'.date("Ymd").'/thumb/';
		if(!is_dir($config[$app]['path'].$path)) {
			if(!FileSystem::mkdir($config[$app]['path'].$path)) $this->displayJSON(array('status' => -3, 'message' => '自动创建目录失败'));
		}
		foreach ($images as $image) {
			$im = new Image($config[$app]['path'] . $image);
			//生成图片
			$im->resizeImage(400, 300);
			$filename = $im->save(3, $config[$app]['path'].$path);
			if($filename) {
				$cols[] = array(
					'original_path' => $image,
					'path' => $path.$filename
				);
			}
		}
		
		$mPicture = new Picture_model();
		$result = $mPicture->add($id, $cols);
		if(!$result) $this->displayJSON(array('status' => -3, 'message' => '保存景点图片失败'));
		else $this->displayJSON(array('status' => 0, 'message' => '保存景点图片成功'));
	}
}